{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "############## PLEASE RUN THIS CELL FIRST! ###################\n",
    "\n",
    "# import everything and define a test runner function\n",
    "from importlib import reload\n",
    "from helper import run\n",
    "import ecc\n",
    "import helper\n",
    "\n",
    "from ecc import FieldElement"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from ecc import FieldElement\n",
    "a = FieldElement(7, 13)\n",
    "b = FieldElement(6, 13)\n",
    "print(a == b)\n",
    "print(a == a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 1\n",
    "\n",
    "Write the corresponding method `__ne__` which checks if two `FieldElement` objects are _not equal_ to each other.\n",
    "\n",
    "#### Make [this test](/edit/code-ch01/ecc.py) pass: `ecc.py:FieldElementTest:test_ne`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Exercise 1\n",
    "\n",
    "reload(ecc)\n",
    "run(ecc.FieldElementTest(\"test_ne\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(7 % 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(-27 % 13)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 2\n",
    "\n",
    "Solve these problems in \\\\(F_{57}\\\\) (assume all +'s here are \\\\(+_{f}\\\\) and -`s here \\\\(-_{f}\\\\))\n",
    "\n",
    "* 44+33\n",
    "* 9-29\n",
    "* 17+42+49\n",
    "* 52-30-38"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Exercise 2\n",
    "\n",
    "# remember that % is the modulo operator\n",
    "prime = 57\n",
    "# 44+33\n",
    "# 9-29\n",
    "# 17+42+49\n",
    "# 52-30-38"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from ecc import FieldElement\n",
    "a = FieldElement(7, 13)\n",
    "b = FieldElement(12, 13)\n",
    "c = FieldElement(6, 13)\n",
    "print(a+b==c)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 3\n",
    "\n",
    "Write the corresponding `__sub__` method which defines the subtraction of two `FieldElement` objects.\n",
    "\n",
    "#### Make [this test](/edit/code-ch01/ecc.py) pass: `ecc.py:FieldElementTest:test_sub`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Exercise 3\n",
    "\n",
    "reload(ecc)\n",
    "run(ecc.FieldElementTest(\"test_sub\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 4\n",
    "\n",
    "Solve the following equations in \\\\(F_{97}\\\\) (again, assume ⋅ and exponentiation are field versions):\n",
    "\n",
    "* 95⋅45⋅31\n",
    "* 17⋅13⋅19⋅44\n",
    "* \\\\(12^{7}\\\\)⋅\\\\(77^{49}\\\\)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Exercise 4\n",
    "\n",
    "prime = 97\n",
    "\n",
    "# 95*45*31\n",
    "# 17*13*19*44\n",
    "# 12**7*77**49"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 5\n",
    "\n",
    "For k = 1, 3, 7, 13, 18, what is this set in \\\\(F_{19}\\\\)?\n",
    "\n",
    "{k⋅0, k⋅1, k⋅2, k⋅3, ... k⋅18}\n",
    "\n",
    "Do you notice anything about these sets?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Exercise 5\n",
    "\n",
    "prime = 19\n",
    "k = 1  # 3, 7, 13 and 18 are the other possibilities\n",
    "# loop through all possible k's 0 up to prime-1\n",
    "# calculate k*iterator % prime\n",
    "\n",
    "# Hint - sort!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from ecc import FieldElement\n",
    "a = FieldElement(3, 13)\n",
    "b = FieldElement(12, 13)\n",
    "c = FieldElement(10, 13)\n",
    "print(a*b==c)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 6\n",
    "\n",
    "Write the corresponding `__mul__` method which defines the multiplication of two Finite Field elements.\n",
    "\n",
    "#### Make [this test](/edit/code-ch01/ecc.py) pass: `ecc.py:FieldElementTest:test_mul`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Exercise 6\n",
    "\n",
    "reload(ecc)\n",
    "run(ecc.FieldElementTest(\"test_mul\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from ecc import FieldElement\n",
    "a = FieldElement(3, 13)\n",
    "b = FieldElement(1, 13)\n",
    "print(a**3==b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 7\n",
    "\n",
    "For p = 7, 11, 17, 31, what is this set in \\\\(F_{p}\\\\)?\n",
    "\n",
    "{\\\\(1^{(p-1)}\\\\), \\\\(2^{(p-1)}\\\\), \\\\(3^{(p-1)}\\\\), \\\\(4^{(p-1)}\\\\), ... \\\\((p-1)^{(p-1)}\\\\)}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Exercise 7\n",
    "\n",
    "primes = [7, 11, 17, 31, 43]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 8\n",
    "\n",
    "Solve the following equations in \\\\(F_{31}\\\\):\n",
    "\n",
    "* 3 / 24\n",
    "* \\\\(17^{-3}\\\\)\n",
    "* \\\\(4^{-4}\\\\)⋅11"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Exercise 8\n",
    "\n",
    "# 3/24\n",
    "# 17**-3\n",
    "# 4**-4*11"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 9\n",
    "\n",
    "Write the corresponding `__truediv__` method which defines the division of two field elements.\n",
    "\n",
    "Note that in Python3, division is separated into `__truediv__` and `__floordiv__`. The first does normal division, the second does integer division.\n",
    "\n",
    "#### Make [this test](/edit/code-ch01/ecc.py) pass: `ecc.py:FieldElementTest:test_div`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Exercise 9\n",
    "\n",
    "reload(ecc)\n",
    "run(ecc.FieldElementTest(\"test_div\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from ecc import FieldElement\n",
    "a = FieldElement(7, 13)\n",
    "b = FieldElement(8, 13)\n",
    "print(a**-3==b)"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
